﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Xml;

/// <summary>
/// Exception handler
/// </summary>
/// 
namespace Shut {
	public class ShException : WebException
	{
		public ShException()
		{
			
		}
		public bool IsAjax { get; protected set; }
		public string ErrorDetails;
		protected string stackTrace;
		public bool isPageNotFound {get; protected set;}
	

		public ShException(string message, bool isAjax)
			: base(message)
		{
			this.IsAjax = isAjax;
			this.ErrorDetails = message;
			
		}
		public ShException(string message, string ErrorDetails, bool isAjax)
			: base(message)
		{
			this.IsAjax = isAjax;
			this.ErrorDetails = ErrorDetails;
			
		}
		public ShException(string message, Exception innerException, bool isAjax) :
			base(message, innerException) 
		{
			// if created from app_error im losing the stacktrace!
				
			this.IsAjax = isAjax;
			this.ErrorDetails = innerException.Message;
			this.stackTrace = innerException.StackTrace;
			// TODO: what do i do with the message

			if (innerException.GetType() == typeof(WebException)){
				this.ErrorDetails += "\nStatus: "+ ((WebException)innerException).Status.ToString();
					WebException exnew = (WebException)innerException;
				if (exnew.Status == WebExceptionStatus.ProtocolError && exnew.Response != null){
					var resp = (HttpWebResponse)exnew.Response;
					if (resp.StatusCode == HttpStatusCode.NotFound){
						this.isPageNotFound = true;
					}
				}
			}
			if (innerException.GetType() == typeof(HttpException)){
				this.ErrorDetails += "\nCode: " + ((HttpException)innerException).GetHttpCode().ToString();
				if(((HttpException)innerException).GetHttpCode() == 404){
					this.isPageNotFound = true;
				}
			}

		}

		
	}
}